package com.cheetah.rest;

import com.cheetah.client.CheetahClient;
import com.cheetah.constants.ProtobufConstants;
import com.cheetah.dto.LoginDTO;
import com.cheetah.dto.UserDTO;
import com.cheetah.protocol.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName ProbuffClient
 * @Description: protobuf协议客户端
 * @Author Administrator
 * @Date 2019/7/22
 * @Version V1.0
 **/
@RestController
@RequestMapping("/cheetah")
@Slf4j
public class ProtobufRest {

    @Autowired
    private CheetahClient cheetahClient;

    @PostMapping("/login")
    public void login(){
        LoginDTO loginDTO = new LoginDTO();
        loginDTO.setAccount("hxw");
        loginDTO.setPassword("111");
        Response response = (Response)cheetahClient.sendMessage(loginDTO,ProtobufConstants.LOGIN);

        log.info(response.toString());
    }

    @PostMapping("/logout")
    public void logout(HttpServletRequest request){
        String token = request.getHeader("token");
        UserDTO userDTO = new UserDTO();
        userDTO.setToken(token);
        Response response = (Response)cheetahClient.sendMessage(userDTO,ProtobufConstants.LOGOUT);

        log.info(response.toString());
    }

    @PostMapping("/group")
    public void group(HttpServletRequest request){
        String token = request.getHeader("token");
        UserDTO userDTO = new UserDTO();
        userDTO.setToken(token);
        Response response = (Response)cheetahClient.sendMessage(userDTO,ProtobufConstants.CREATE_GROUP);

        log.info(response.toString());
    }
}
